package top.yuhaitao.core.util;

import com.google.gson.Gson;
import okhttp3.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.*;

public class WechatUtil {
    private final static String dev = "gh_ab122d3f7a4d";
    private final static String appid = "wxdeb6767d0c011d8d";
    private final static String secret = "9a98e1ad6586689c9925401f1f1f337f";

    private final static String WECHAT_HEALCHECK_TOKEN = "wechat_token";
    private final static String ENCODING_AES_KEY = "N8JKTf4n6qIxYLPl2Xcqb5KH96tPUXQXtjXG6NYrXri";


    /**
     * 验签
     *
     * @param token
     * @param nonce
     * @param timestamp
     * @param signature
     * @return
     */
    public static boolean validateSignature(String token, String nonce, String timestamp, String signature) {
        List<String> arr = Arrays.asList(token, nonce, timestamp);
        arr.sort(String::compareTo);
        StringBuilder str = new StringBuilder();
        for (String s : arr) {
            str.append(s);
        }
        String s = DigestUtils.sha1Hex(str.toString());
        return signature.equals(s);
    }

    /**
     * 获取access_token
     */
    public static String getAccesstoken() throws IOException {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        String api = url.replaceAll("APPID", appid).replaceAll("APPSECRET", secret);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(api)
                .get()
                .build();
        Response response = client.newCall(request).execute();
        assert response.body() != null;
        String retStr = response.body().string();
        System.out.println(retStr);
        return retStr;
    }

    /**
     * 获取access_token
     */
    public static String createQrcode(String accesstoken) throws IOException {
        String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN";
        String api = url.replaceAll("TOKEN", accesstoken);
        Map<String, String> map1 = new HashMap<>();
        Map<String, String> map2 = new HashMap<>();
        Map<String, String> map3 = new HashMap<>();
        map3.put("scene_str", UUID.randomUUID().toString());
        map2.put("scene", new Gson().toJson(map3));
        map1.put("action_name", "QR_LIMIT_STR_SCENE");
        map1.put("action_info", new Gson().toJson(map2));
        String json = new Gson().toJson(map1);
        System.out.println(json);

        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(api)
                .post(body)
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();
        assert response.body() != null;
        String retStr = response.body().string();
        System.out.println(retStr);
        return retStr;
    }


    public static void main(String[] args) throws IOException {
//        getAccesstoken();
        //18_LVKAtxhi7l-BQxNuZnuOOT6b43sLwyXxBTqAoE3sXno7GParrw-U8zdbWiCHTqDMQVWI-GDsux8MEpu3Dsyi0zzbI96KHK5hY2QgfVAc_Q5TFx4JNddhxbKaYz23C5BLiyBBZQs9fNbxZtlGXCXiAJAGLG
        String qrcode = createQrcode("18_LVKAtxhi7l-BQxNuZnuOOT6b43sLwyXxBTqAoE3sXno7GParrw-U8zdbWiCHTqDMQVWI-GDsux8MEpu3Dsyi0zzbI96KHK5hY2QgfVAc_Q5TFx4JNddhxbKaYz23C5BLiyBBZQs9fNbxZtlGXCXiAJAGLG");
        System.out.println(qrcode);
    }

}
